package com.it2297.controller;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import com.it2297.main.App;
import com.it2297.model.Bid;
import com.it2297.model.UsedItem;
import com.it2297.view.BiddingHistoryView;
import com.it2297.view.DonationHistoryView;

public class DonationHistoryController
{
	private DonationHistoryView historyView;

	public DonationHistoryController(DonationHistoryView historyView)
	{
		this.historyView = historyView;

		historyView.addTreeMouseActionListener(new HistoryViewTreeMouseListener());
	}

	private class HistoryViewTreeMouseListener extends MouseAdapter
	{
		@Override
		public void mouseClicked(MouseEvent e)
		{
			if (!historyView.isTreeSelectionEmpty())
			{
				try
				{
					String monthStr = historyView.getSelectedTreeValue();
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(new SimpleDateFormat("MMM").parse(monthStr));
					int monthInt = calendar.get(Calendar.MONTH) + 1;

					ArrayList<UsedItem> usedItems = UsedItem.getMultiple(App.currentUser.getNric(), monthInt);

					historyView.clearTableRow();

					for (UsedItem usedItem : usedItems)
					{
						historyView.addTableRow(usedItem);
					}

				}
				catch (ParseException e1)
				{
					System.out.println("Not a month format MMM.");
				}
			}
		}
	}
}
